package com.jiutong.meipengdai.http;

import com.jiutong.meipengdai.BuildConfig;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Retrofit 初始化
 * Created by suxi on 17/5/9.
 */
public class RetrofitHandle {

    private static final int DEFAULT_TIMEOUT = 10;
    //        public static final String BASE_URL = "http://192.168.1.178:8080/mpd-api/";//微醺
//    public static final String BASE_URL = "http://192.168.1.107:8080/mpd-api/";
//    public static final String BASE_URL = "http://192.168.1.88:8088/mpd-api/";//顺中
//        public static String BASE_URL = "http://192.168.1.119:8081/mpd-api/"; //
        public static String BASE_URL = "http://192.168.1.136:8989/mpd-api/"; //测试
//    public static String BASE_URL = "http://119.10.82.22:8080/mpd-api/"; //线上2.0及之前
//    public static String BASE_URL = "http://39.108.238.25:8089/mpd-api/"; //线上3.0及之前
    public Retrofit retrofit;

    //构造方法私有
    private RetrofitHandle() {
        initRetrofit();
    }

    //获取单例
    public static RetrofitHandle getInstance() {
        return SingletonHolder.INSTANCE;
    }

    private void initRetrofit() {
        //添加统一的log管理，打release包的时候记得将Level设置成Level.NONE
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();

        if (BuildConfig.LOG_DEBUG) {
            loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        } else {
            loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.NONE);
        }

        OkHttpClient.Builder builder = new OkHttpClient.Builder().addInterceptor(loggingInterceptor);

        //手动创建一个OkHttpClient并设置超时时间
        builder.connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS);

        retrofit = new Retrofit.Builder()
                .client(builder.build())
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .baseUrl(BASE_URL)
//                .baseUrl(TextUtils.isEmpty(SpConfig.getInstance().getString(Constants.BASEURL_TEST)) ? BASE_URL : SpConfig.getInstance().getString(Constants.BASEURL_TEST))
                .build();

    }

    //在访问HttpMethods时创建单例
    private static class SingletonHolder {
        private static final RetrofitHandle INSTANCE = new RetrofitHandle();
    }
}
